package com.fdb.efp.nls.api.service;

import com.fdb.basic.framework.core.cache.Cache;
import com.fdb.basic.framework.core.cache.PiccsFrameworkCache;
import com.fdb.basic.framework.core.util.BeanUtility;
import com.fdb.basic.framework.core.util.SpringContextUtils;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.core.vo.OrgInfoCacheVO;
import com.fdb.basic.framework.core.vo.UserInfoCacheVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.fdb.basic.framework.core.constant.CacheConstant.CACHE_KEY_ORG_INFOS;
import static com.fdb.basic.framework.core.constant.CacheConstant.CACHE_KEY_USER_INFOS;

/**
 * 需求编号【2019D0509】
 *
 * @author qiuyf
 * @version 0.1 初始为0.1,后续变更则修改
 * @since 2019/12/10 9:10
 */
@Service("nlsBizCodeReverseNameService")
public class NlsBizCodeReverseNameService {
    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(NlsBizCodeReverseNameService.class);

    public final <T> T displayOrgName(T data, String srcColumn, String targetColumn) {
        try {
            // 获取机构缓存
            PiccsFrameworkCache bean = (PiccsFrameworkCache) SpringContextUtils.getBean(Cache.CACHE_BEAN);
            Map<String, OrgInfoCacheVO> orgInfoCacheVOMap = bean.get(CACHE_KEY_ORG_INFOS);
            if (Objects.nonNull(orgInfoCacheVOMap) && Objects.nonNull(data) && StringUtil.isNotEmpty(srcColumn) && StringUtil.isNotEmpty(targetColumn)) {
                if (data instanceof List) {
                    List list = (List) data;
                    for (Object o : list) {
                        String code = StringUtil.replaceObjNull(BeanUtility.getProptery(o, srcColumn));
                        if (StringUtil.isNotEmpty(code)) {
                            Object cacheObject = orgInfoCacheVOMap.get(code);
                            if (Objects.nonNull(cacheObject)) {
                                Object displayName = BeanUtility.getProptery(cacheObject, "orgName");
                                BeanUtility.setProptery(o, targetColumn, displayName);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("当前字段：" + srcColumn + ", 目标字段：" + targetColumn + "转换翻译出现异常!", e);
        }
        return data;
    }

    public final <T> T displayUserName(T data, String srcColumn, String targetColumn) {
        try {
            // 获取机构缓存
            PiccsFrameworkCache bean = (PiccsFrameworkCache) SpringContextUtils.getBean(Cache.CACHE_BEAN);
            Map<String, UserInfoCacheVO> userInfoCacheVOMap = bean.get(CACHE_KEY_USER_INFOS);
            if (Objects.nonNull(userInfoCacheVOMap) && Objects.nonNull(data) && StringUtil.isNotEmpty(srcColumn) && StringUtil.isNotEmpty(targetColumn)) {
                if (data instanceof List) {
                    List list = (List) data;
                    for (Object o : list) {
                        String code = StringUtil.replaceObjNull(BeanUtility.getProptery(o, srcColumn));
                        if (StringUtil.isNotEmpty(code)) {
                            Object cacheObject = userInfoCacheVOMap.get(code);
                            if (Objects.nonNull(cacheObject)) {
                                Object displayName = BeanUtility.getProptery(cacheObject, "userName");
                                BeanUtility.setProptery(o, targetColumn, displayName);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("当前字段：" + srcColumn + ", 目标字段：" + targetColumn + "转换翻译出现异常!", e);
        }
        return data;
    }
}
